package orders;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Customer store list order, membertype, name of customer
 * Sort list order with date create
 * get Order, write and read customer from file
 */
public class Customer implements Serializable, Runnable {
	
	private static final long serialVersionUID = 1L;
	
	private String name;
	private String memberType;
	private List<Order> orders = new ArrayList<Order>();
	
	public static final String VIP = "VIP";
	public static final String MEMBER = "MEMBER";
	public static final String ORTHER = "ORTHER";
	
	
	public Customer(String name, String memberType) {
		super();
		this.name = name;
		this.memberType = memberType;
	}



	public Customer() {
		super();
		this.memberType = ORTHER;
		memberType = "Orther";
	}


	/**
	 * @return the memberType
	 */
	public String getMemberType() {
		return memberType;
	}



	/**
	 * @param memberType the memberType to set
	 */
	public void setMemberType(String memberType) {
		this.memberType = memberType;
	}



	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}


	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}


	/**
	 * @return the orders
	 */
	public Collection<Order> getOrders() {
		return orders;
	}


	/**
	 * @param orders the orders to set
	 */
	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}

	/*
	 * Sort list orders with date create
	 * when sort all access to customer is block
	 */
	synchronized public void sort()
	{
		Collections.sort(orders);
	}
	
	public void addOrder(Order order)
	{
		orders.add(order);
	}
	
	/*
	 * Print all orders of customer to console
	 * when print all access to customer is block
	 */
	synchronized public void getAllOrder()
	{
		System.out.println("Name: " + this.getName() + "\t" + this.getMemberType());
		
		double totalPrice = 0;
		int count = 0;
		
		Iterator<Order> lstOrder = orders.iterator();
		while (lstOrder.hasNext())
		{
			Order order = lstOrder.next();
			order.getPrice();
			totalPrice += order.getTotalPrice();
			count++;
		}
		
		System.out.println("Total order: " + count);
		System.out.println("Total price: " + totalPrice);
	}
	
	/*
	 * write object customer to file 
	 */
	public void printFile(String fileName)
	{
		FileOutputStream file;
		ObjectOutputStream oos = null;
		try {
			file = new FileOutputStream(fileName);
			oos = new ObjectOutputStream(file);
			
			oos.writeObject(this);
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally
		{
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/*
	 * Read customer from file, return Customer
	 */
	public static Customer readFile(String fileName)
	{
		FileInputStream file;
		ObjectInputStream ois = null;
		Customer result = null;
		
		try {
			file = new FileInputStream(fileName);
			ois = new ObjectInputStream(file);
			result = (Customer) ois.readObject();
			
		} catch (ClassNotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				ois.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}



	/*
	 * Use in test thread
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		Order intockOrder = new IntockOrder("Thu Duc");
		Order onlineOrder = new OnlineOrder();
		Order order3 = new IntockOrder("Tan Binh");
		Order order4 = new OnlineOrder();
		
		Item item1 = new Item("milk", 6500, 0, 3, "box");
		Item item2 = new Item("coffee", 2000, 0, 20, "box");
		Item item3 = new Item("banana", 10000, 7.0, 3, "kg");
		Item item4 = new Item("tomato", 20000, 10, 3, "kg");
		
		intockOrder.addItem(item1);
		intockOrder.addItem(item2);
		intockOrder.addItem(item3);
		
		onlineOrder.addItem(item1);
		onlineOrder.addItem(item3);
		onlineOrder.addItem(item4);
		onlineOrder.setDiscount(5);
		
		order3.addItem(item1);
		order3.addItem(item3);
		
		order4.addItem(item4);
		
		this.addOrder(order4);
		this.addOrder(order3);
		this.addOrder(onlineOrder);
		this.addOrder(intockOrder);
		
		System.out.println("Truoc getAllOrder");
		
		this.getAllOrder();
		
	}

}
